﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ContentType.cs" company="Lambros Vasiliou">
//   © 2013
// </copyright>
// <summary>
//   Content Type Data Item.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace LV.EnhancedNew.Controls
{
    using System.Globalization;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;

    /// <summary>
    /// Content Type Data Item.
    /// </summary>
    public class ContentType
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ContentType"/> class.
        /// </summary>
        /// <param name="contentType">The source Content Type.</param>
        /// <param name="saveFolder">The folder where documents are to be saved by the client at.</param>
        public ContentType(SPContentType contentType, string saveFolder)
        {
            string templateName = contentType.DocumentTemplate;
            string icon16 = SPUtility.MapToIcon(SPContext.Current.Web, templateName, string.Empty, IconSize.Size16);
            string icon32 = SPUtility.MapToIcon(SPContext.Current.Web, templateName, string.Empty, IconSize.Size32);
            string icon256 = SPUtility.MapToIcon(SPContext.Current.Web, templateName, string.Empty, IconSize.Size256);

            this.Icon16Url = SPContext.Current.Site.MakeFullUrl(Constants.IconImagesFolderUrl + icon16);
            this.Icon32Url = SPContext.Current.Site.MakeFullUrl(Constants.IconImagesFolderUrl + icon32);
            this.Icon256Url = SPContext.Current.Site.MakeFullUrl(Constants.IconImagesFolderUrl + icon256);
            this.AppName = SPUtility.MapToApp(SPContext.Current.Web, templateName, string.Empty);
            this.ControlName = SPUtility.MapToControl(SPContext.Current.Web, templateName, string.Empty);
            this.TemplateUrl = SPContext.Current.Site.MakeFullUrl(contentType.DocumentTemplateUrl);
            this.Id = contentType.Id.ToString();
            this.Name = contentType.Name;
            this.Description = contentType.Description;
            this.Group = contentType.Group;
            this.TemplateName = templateName;
            SPSite site = contentType.ParentWeb.Site;
            var listRootFolderUrl = contentType.ParentList.RootFolder.ServerRelativeUrl;
            var saveLocation = !string.IsNullOrWhiteSpace(saveFolder) ? site.MakeFullUrl(saveFolder) : site.MakeFullUrl(listRootFolderUrl);
            var newDocumentUrl = site.MakeFullUrl(Constants.CreateNewDocumentAspxUrl);
            var encodedNewDocumentUrl = SPEncode.ScriptEncode(newDocumentUrl);
            var encodedDocumentTemplateUrl = SPEncode.ScriptEncode(this.TemplateUrl);

            var encodedSaveUrl = SPEncode.ScriptEncode(saveLocation);

            this.NewDocumentScript = string.Format(CultureInfo.InvariantCulture, Constants.NewDocumentScriptCommandTemplate, new object[] { encodedDocumentTemplateUrl, encodedSaveUrl, this.ControlName, encodedNewDocumentUrl, this.AppName });
        }

        /// <summary>
        /// Gets or sets the ID of the Content Type.
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// Gets or sets the Name of the Content Type.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the Description of the Content Type.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the 16x16 pixel Icon for the template type.
        /// </summary>
        public string Icon16Url { get; set; }

        /// <summary>
        /// Gets or sets the 32x32 pixel Icon for the template type.
        /// </summary>
        public string Icon32Url { get; set; }

        /// <summary>
        /// Gets or sets the 256x256 pixel Icon for the template type.
        /// </summary>
        public string Icon256Url { get; set; }

        /// <summary>
        /// Gets or sets the name of the client application that can edit the specified template file.
        /// </summary>
        public string AppName { get; set; }

        /// <summary>
        /// Gets or sets the name of the control that can edit the specified template file, including the website that contains the file and ID of the program that created the file.
        /// </summary>
        public string ControlName { get; set; }

        /// <summary>
        /// Gets or sets the full URL to the document template for this Content Type.
        /// </summary>
        public string TemplateUrl { get; set; }

        /// <summary>
        /// Gets or sets the file name for the Content Type template.
        /// </summary>
        public string TemplateName { get; set; }

        /// <summary>
        /// Gets the client script that is used to generate a new document for this Content Type.
        /// </summary>
        public string NewDocumentScript { get; private set; }

        /// <summary>
        /// Gets or sets the group for the Content Type
        /// </summary>
        public string Group { get; set; }
    }
}
